<%@ page language="java" pageEncoding="UTF-8" contentType="text/html; charset=utf-8" errorPage="/error.jsp" isELIgnored="false" %>
<%@ taglib uri="http://jakarta.apache.org/struts/tags-bean" prefix="bean" %>
<%@ taglib uri="http://jakarta.apache.org/struts/tags-html" prefix="html" %>
<%@ taglib uri="http://jakarta.apache.org/struts/tags-logic" prefix="logic" %>
<%@ taglib uri="http://jakarta.apache.org/struts/tags-tiles" prefix="tiles" %>
<%@ taglib uri="http://java.sun.com/jstl/core" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jstl/fmt" prefix="fmt" %>
<%@ taglib uri="http://www.opensymphony.com/oscache" prefix="cache" %>
<%@ taglib uri="http://atleap.blandware.com/taglib" prefix="atleap" %>
<html:xhtml />

<atleap:constants className="com.blandware.atleap.webapp.util.core.WebappConstants" var="GRID_PAGE_SIZE" />
<atleap:globalProperty name="${pageScope.GRID_PAGE_SIZE}" defaultValue="10" type="integer" var="pageSize" />

<atleap:isUserInRole roles="forum-post-delete" var="canDelete" />
<atleap:isUserInRole roles="forum-post-viewEmail" var="canViewEmail" />

<jsp:include page="/WEB-INF/pages/forum/pathFragment.jsp" />

<atleap:messages />
<atleap:grid name="postGrid" pageSize="${pageSize}">
    <atleap:queryParameter name="topicId" value="${currentTopic.id}" />

    <atleap:form action="forum/post/callCreate" method="POST" validateOnSubmit="false">
        <atleap:submit styleClass="button"><atleap:message key="forum.post.buttons.create" /></atleap:submit>
    </atleap:form>

    <atleap:message key="forum.post.buttons.reply" var="replyTitle" />
    <atleap:message key="core.commons.buttons.delete" var="deleteTitle" />
    <c:set var="first" value="true" />
    <table border="0" cellpadding="0" cellspacing="0" class="postList" id="postList">
        <atleap:rowsIterator beanId="forumManager" method="listPosts" var="post" oddClass="oddPost" evenClass="evenPost">
            <%-- Trick to draw pager BEFORE rows, but only one time --%>
            <c:if test="${first}">
            <atleap:pager pageCount="10">
                <tr class="topicPager">
                    <td colspan="2" style="border-bottom: none;">
                        <strong><atleap:message key="core.grid.pager.title" /></strong>&nbsp;
                        <atleap:firstPage><img src="<atleap:rewriteUrl href="/images/theme/${appConfig['frontendCssTheme']}/first.gif" />" border="0" alt="<atleap:message key="core.grid.pager.first"/>" title="<atleap:message key="core.grid.pager.first"/>" /></atleap:firstPage>
                        <atleap:prevPage><img src="<atleap:rewriteUrl href="/images/theme/${appConfig['frontendCssTheme']}/prev.gif" />" border="0" alt="<atleap:message key="core.grid.pager.prev"/>" title="<atleap:message key="core.grid.pager.prev"/>" /></atleap:prevPage>
                        <atleap:pagesIterator>
                            <atleap:page>${pageNumber}&nbsp;</atleap:page>
                            <atleap:currentPage><b>${pageNumber}</b>&nbsp;</atleap:currentPage>
                        </atleap:pagesIterator>
                        <atleap:nextPage><img src="<atleap:rewriteUrl href="/images/theme/${appConfig['frontendCssTheme']}/next.gif" />" border="0" alt="<atleap:message key="core.grid.pager.next"/>" title="<atleap:message key="core.grid.pager.next"/>" /></atleap:nextPage>
                        <atleap:lastPage><img src="<atleap:rewriteUrl href="/images/theme/${appConfig['frontendCssTheme']}/last.gif" />" border="0" alt="<atleap:message key="core.grid.pager.last"/>" title="<atleap:message key="core.grid.pager.last"/>" /></atleap:lastPage>
                    </td>
                <tr>
            </atleap:pager>
            </c:if>
            <c:set var="first" value="false" />
            <tr class="${rowClass}">
                <td width="20%" valign="top" align="center">
                    <c:out value="${post.author}" escapeXml="true" />
                    <c:if test="${canViewEmail}">
                        <c:set var="email" value="${post.email}" />
                        <c:if test="${!empty email}">
                            <c:set var="escapedEmail">
                                <c:out value="${email}" escapeXml="true" />
                            </c:set>
                            <br />
                            (<a href="mailto:${escapedEmail}">${escapedEmail}</a>)
                        </c:if>
                    </c:if>
                </td>
                <td>
                    <table width="100%" border="0" cellspacing="0" cellpadding="0" class="postInternalTable">
                        <tr>
                            <td class="postTitleCell">
                                <atleap:formatDate value="${post.date}" type="datetime" />
                                <strong><atleap:message key="forum.post.form.subject" />:</strong>
                                <c:out value="${post.subject}" escapeXml="true" />
                            </td>
                        </tr>
                        <tr>
                            <td class="postInternalCell">
                                ${post.body}
                            </td>
                        </tr>
                        <tr>
                            <td class="postInternalCell">
                                <atleap:link action="forum/post/callCreate" title="${replyTitle}">
                                    <atleap:param name="postId" value="${post.id}" />
                                    ${replyTitle}
                                </atleap:link>
                                <c:if test="${canDelete && !post.first}">
                                    <atleap:link action="forum/post/delete" transaction="true" title="${deleteTitle}" onclick="return confirmDelete();">
                                        <atleap:param name="id" value="${post.id}" />
                                        ${deleteTitle}
                                    </atleap:link>
                                </c:if>
                            </td>
                        </tr>
                    </table>
                </td>
            </tr>
        </atleap:rowsIterator>
        <atleap:pager pageCount="10">
            <tr class="topicPager">
                <td colspan="2">
                    <strong><atleap:message key="core.grid.pager.title" /></strong>&nbsp;
                    <atleap:firstPage><img src="<atleap:rewriteUrl href="/images/theme/${appConfig['frontendCssTheme']}/first.gif" />" border="0" alt="<atleap:message key="core.grid.pager.first"/>" title="<atleap:message key="core.grid.pager.first"/>" /></atleap:firstPage>
                    <atleap:prevPage><img src="<atleap:rewriteUrl href="/images/theme/${appConfig['frontendCssTheme']}/prev.gif" />" border="0" alt="<atleap:message key="core.grid.pager.prev"/>" title="<atleap:message key="core.grid.pager.prev"/>" /></atleap:prevPage>
                    <atleap:pagesIterator>
                        <atleap:page>${pageNumber}&nbsp;</atleap:page>
                        <atleap:currentPage><b>${pageNumber}</b>&nbsp;</atleap:currentPage>
                    </atleap:pagesIterator>
                    <atleap:nextPage><img src="<atleap:rewriteUrl href="/images/theme/${appConfig['frontendCssTheme']}/next.gif" />" border="0" alt="<atleap:message key="core.grid.pager.next"/>" title="<atleap:message key="core.grid.pager.next"/>" /></atleap:nextPage>
                    <atleap:lastPage><img src="<atleap:rewriteUrl href="/images/theme/${appConfig['frontendCssTheme']}/last.gif" />" border="0" alt="<atleap:message key="core.grid.pager.last"/>" title="<atleap:message key="core.grid.pager.last"/>" /></atleap:lastPage>
                </td>
            <tr>
        </atleap:pager>
    </table>

    <atleap:form action="forum/post/callCreate" method="POST" validateOnSubmit="false">
        <atleap:submit styleClass="button"><atleap:message key="forum.post.buttons.create" /></atleap:submit>
    </atleap:form>
</atleap:grid>
